package cc.fs.zhihuapp.utils;

import android.view.SurfaceHolder;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Created by fostion on 2015/10/30.
 */
public class Player implements IMediaPlayer.OnCompletionListener,IMediaPlayer.OnPreparedListener,IMediaPlayer.OnBufferingUpdateListener{

    private IjkMediaPlayer ijkMediaPlayer;
    private PlayerListener playerListener;
    private MediaPlayerProxy proxy;
    private Timer timer = new Timer();

    boolean useProxy = true;

    public Player(){
        ijkMediaPlayer = new IjkMediaPlayer();
        ijkMediaPlayer.setOnCompletionListener(this);
        ijkMediaPlayer.setOnPreparedListener(this);
        ijkMediaPlayer.setOnBufferingUpdateListener(this);
    }

    public void playUrl(String url){

        if (useProxy) {
            startProxy();
            url = proxy.getProxyURL(url);
        }

        try {
            //todo 开启代理
            ijkMediaPlayer.setDataSource(url);
            ijkMediaPlayer.setLooping(false);
            ijkMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void setSurcefaceHolder(SurfaceHolder _holder){
        ijkMediaPlayer.setDisplay(_holder);
    }

    public boolean isPlaying(){
        return ijkMediaPlayer.isPlaying();
    }

    public void start(){
        ijkMediaPlayer.start();
    }

    public void pause(){
        ijkMediaPlayer.pause();
    }

    public void stop(){
        ijkMediaPlayer.stop();
    }

    public void relase(){
        if(ijkMediaPlayer.isPlaying())
            ijkMediaPlayer.stop();
        timer.cancel();
        ijkMediaPlayer.release();
    }

    public void setUseProxy(boolean useProxy) {
        this.useProxy = useProxy;
    }

    public void setPlayerListener(PlayerListener _PlayerListener){
        this.playerListener = _PlayerListener;
    }

    public PlayerListener getPlayerListener() {
        return playerListener;
    }

    @Override
    public void onBufferingUpdate(IMediaPlayer mp, int percent) {
        if(playerListener != null)
            playerListener.onBuffered(percent);
    }

    @Override
    public void onCompletion(IMediaPlayer mp) {
        if(ijkMediaPlayer != null)
            playerListener.onCompletion();
    }

    @Override
    public void onPrepared(IMediaPlayer mp) {
        if(playerListener != null)
            playerListener.onPrepared();

        mp.start();
        timer.schedule(mTimerTask,0,500);
    }

    public void seekTo(int position){
        int cTime = position;
        int nTime =  ((Number) ijkMediaPlayer.getCurrentPosition()).intValue();
        long offsetTime = ijkMediaPlayer.getCurrentPosition() + cTime + nTime;
        ijkMediaPlayer.seekTo(offsetTime);
    }

    int getOffsetTime(int clickTime, int nowTime) {
        int cTime = clickTime;
        int nTime = nowTime;
        return cTime - nTime;
    }

    public void seekBack(){
        if(isPlaying()){
            long position = ijkMediaPlayer.getCurrentPosition() - 20000;
            ijkMediaPlayer.seekTo(position);
        }
    }

    public void seekForward(){
        if(isPlaying()){
            long position = ijkMediaPlayer.getCurrentPosition() + 20000;
            ijkMediaPlayer.seekTo(position);
        }
    }

    /*******************************************************
     * 通过定时器获取时间
     ******************************************************/
    TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            if (ijkMediaPlayer == null)
                return;
            if (ijkMediaPlayer.isPlaying()) {
                int position = ((Number)ijkMediaPlayer.getCurrentPosition()).intValue();
                int duration =((Number) ijkMediaPlayer.getDuration()).intValue();

                if(playerListener != null) {
                    playerListener.onProgress(duration, position);
                }
            } else {
                if(playerListener != null){
                    playerListener.onBuffering();
                }
            }
        }
    };

    private void startProxy() {
        if (proxy == null) {
            proxy = new MediaPlayerProxy();
            proxy.init();
            proxy.start();
        }
    }

    public interface PlayerListener{
        void onPrepared();
        void onBuffering();
        void onBuffered(int buffered);
        void onProgress(int total,int progress);
        void onCompletion();
    }

}
